﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaunaServer.Model.Logger
{
    /// <summary>
    /// XML Logger class that write a log message into XML log file
    /// </summary>
    /// <remarks>
    /// This class is thread safe. 
    /// </remarks>
    /// <dates>
    /// 15/05/2011 - created by Chiaki Sato
    /// </dates>
    class XmlLogger : ILogger
    {
        private ILoggerMessage stackTraceMessage;
        private ILoggerMessage hostAddressMessage;
        private IXmlDaoGateway xmlDaoGateway;
        private object lockObject;

        public enum LogType { UNIT = 1, COMPO = 2 }

        /// <summary>
        /// Default constructor for XML Logger class.
        /// XML Log files will be created in this application directory as default. 
        /// </summary>
        public XmlLogger(LogType logType)
        {
            lockObject = new object();

            stackTraceMessage = new StackTraceMessage((StackTraceMessage.LogType)logType);
            hostAddressMessage = HostAddressSingleton.Instance;
            xmlDaoGateway = XmlDaoGateway.Instance;
        }

        /// <summary>
        /// Write a log message with stack trace to a xml file
        /// </summary>
        /// <param name="message">User log message</param>
        public string Write(string message)
        {
            string logMessage;

            // For tread safty
            lock (lockObject)
            {
                xmlDaoGateway.AppendMessage("HostAddress", hostAddressMessage.CreateLogMessage());
                xmlDaoGateway.AppendMessage("StackTrace", stackTraceMessage.CreateLogMessage());
                xmlDaoGateway.AppendMessage("UserMessage", message);
                logMessage = xmlDaoGateway.WriteMessage();
            }

            return logMessage;
        }
    }
}
